package com.fanben100.biz;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fanben100.domain.LoginModel;
import com.fanben100.domain.Seller;
import com.fanben100.util.StringUtil;

/**
 * 带验证信息的LoginModel
 * 
 * @author Administrator
 * 
 */
public class ValidLoginModel implements LoginModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String SUCCESS_FLAG = "登录成功";

	/**
	 * 是否为网络连接异常
	 */
	private boolean isNetFault;

	public boolean isNetFault() {
		return isNetFault;
	}

	public void setNetFault(boolean isNetFault) {
		this.isNetFault = isNetFault;
	}

	private LoginModel loginModel;

	public ValidLoginModel(LoginModel loginModel) {
		this.loginModel = loginModel;
	}

	/**
	 * 是否通过验证
	 */
	// private boolean valid;

	/**
	 * 验证信息
	 */
	private String validMessage = "";

	public boolean isValid() {
		if (!StringUtil.isNullOrEmpty(validMessage)) {
			if (validMessage.indexOf(SUCCESS_FLAG) != -1) {
				return true;
			} else {
				return false;
			}
		}
		return false;
		// return valid;
	}

	// public void setValid(boolean valid) {
	// this.valid = valid;
	// }

	public Serializable getLoginId() {
		if (loginModel != null) {
			return loginModel.getLoginId();
		}
		return null;
	}

	public String getLoginPassword() {
		if (loginModel != null) {
			return loginModel.getLoginPassword();
		}
		return null;
	}

	public void setLoginId(Serializable loginId) {
		if (loginModel != null) {
			loginModel.setLoginId(loginId);
		}
	}

	public void setLoginPassword(String loginPassword) {
		if (loginModel != null) {
			loginModel.setLoginPassword(loginPassword);
		}
	}

	public String getRealName() {
		if (loginModel != null) {
			return loginModel.getRealName();
		}
		return null;
	}

	public void setRealName(String realName) {
		if (loginModel != null) {
			loginModel.setRealName(realName);
		}
	}

	public LoginModel getLoginModel() {
		return loginModel;
	}

	public void setLoginModel(LoginModel loginModel) {
		this.loginModel = loginModel;
	}

	public String getValidMessage() {
		return validMessage;
	}

	public void setValidMessage(String validMessage) {
		this.validMessage = validMessage;
		Pattern pattern = Pattern.compile("\\[(.*?)\\]");
		Pattern idPattern = Pattern.compile("\\{(.*?)\\}");
		if (!StringUtil.isNullOrEmpty(validMessage)) {
			Matcher matcher = pattern.matcher(validMessage);
			Matcher idMatcher = idPattern.matcher(validMessage);
			while (matcher.find()) {
				String realName = matcher.group(1);
				this.setRealName(realName);
			}
			while (idMatcher.find()) {
				String sellerId = idMatcher.group(1);
				if (this.loginModel instanceof Seller) {
					((Seller) this.loginModel).setSellerId(Integer
							.valueOf(sellerId));
				}
				// this.setLoginId();
			}
		}
	}

	public String getIdentify() {
		return loginModel.getIdentify();
	}

	public void setIdentify(String identify) {
		this.loginModel.setIdentify(identify);
	}

	public Integer getSellerId() {
		if (this.loginModel != null && this.loginModel instanceof Seller) {
			return ((Seller) this.loginModel).getSellerId();
		}
		return null;
	}

}
